﻿
using MTOM_Library;
using System;
using System.IO;
using System.Threading;
using System.Web.UI;
using System.Windows.Forms;
using UploadWinClient;
using zyconfig;

namespace zymain
{
    public class UploadFiles
    {
        private string _formName;
        private string _uid;
        private TaskPanel taskPanel1;
        public UploadFiles(string uid, string formName)
        {
            this._formName = formName;
            this._uid = uid;
            Form fr = System.Windows.Forms.Application.OpenForms[this._formName];
            taskPanel1 = fr.Controls.Find("taskPanel1", true)[0] as TaskPanel;
        }

        public void AddFileUpLoad(string uType,string FilePath, string MainKay)
        {
            string guid = Guid.NewGuid().ToString();
            string kzName = Path.GetExtension(FilePath).ToUpper().Replace(".", "");
            string fileName = Path.GetFileName(FilePath);
            if (FileFunction.InsertInfor(uType,MainKay, kzName, fileName, this._uid, FilePath, guid))
            {
                this.UploadFile(new Triplet(guid, FilePath, "0|"+uType));
                ControlLog.AddUpLoadLog(uType, this._uid, FilePath, MainKay);
            }
            else
            {
                MessageBox.Show("添加信息到数据库失败！无法上传文件,请检查...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void UploadFile(object triplet)
        {
            string str = (triplet as Triplet).First.ToString();
            string str2 = (triplet as Triplet).Second.ToString();
            string[] strArray = (triplet as Triplet).Third.ToString().Split(new char[] { '|' });
            UploadControl control = new UploadControl();
            control.FileName = str2;
            control.Dock = DockStyle.Top;
            control.guid = str;
            control.UpType = strArray[1];
            this.taskPanel1.Controls.Add(control);
            control.Name = DateTime.Now.ToString("yyyyMMddhhmmss");
            control.UpState = "     上传中...";
            control.UploadFile();
            control.DeleteClick += new UploadControl.btnclick(this.MyLoadControl_DeleteClick);
            this.MyLoadControl_DeleteClick(control, null);
        }

        private void MyLoadControl_DeleteClick(object sender, EventArgs e)
        {
            if (((UploadControl)sender).isFinlish == "完成")
            {
                DeleteFile file;
                Thread thread;
                string guid = (sender as UploadControl).guid;
                string upType = (sender as UploadControl).UpType;
                string filePath = string.Empty;
                //Console.WriteLine(guid + ":" + upType);
                string mainKay = FileFunction.GetMainKay(upType, guid, out filePath);
                //ControlLog.AddUpLoadLog(upType, this._uid, filePath, mainKay);
                if (!FileFunction.UpdateSucess(upType, guid, this._uid))
                {
                    MessageBox.Show("更新上传库信息失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (DbConfig.DelOnUpLoadSucess == "1")
                {
                    file = new DeleteFile(guid, upType);
                    thread = new Thread(new ThreadStart(file.DoWork));
                    thread.Start();
                    if (upType.Equals("0") && !ControlLog.UpdateSqKt(mainKay))
                    {
                        MessageBox.Show("更新申请开通数据失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }

            this.taskPanel1.Controls.Remove((UploadControl)sender);
            ((UploadControl)sender).UpState = "【完成】";
        }
    }
}
